//
//  NoCheckLonginView.swift
//  Kosspot
//
//  Created by 李鑫 on 2025/6/19.
//

import SwiftUI
import ToastUI

struct NoCheckLonginView: View {
    @StateObject private var weChatManager = WeChatManager.shared
    @StateObject var viewModel = LoginViewModel()
    @EnvironmentObject var appState:AppState
    @State private var showToast = false
    @State private var isAgreed = false
    @State private var phoneNumber = "13716888888"
    @State private var compont = "中国移动"
    var body: some View {
        VStack {
            ZStack {
                Image("6528")
                    .resizable()
                    .frame(height: 194)
                
                Image("Kosspot")
                    .resizable()
                    .frame(width: 116,height: 52)
                VStack {
                    Image("6711")
                        .resizable()
                        .frame(width: 143,height: 26)
                        .padding(.top,35)
                        .padding(.leading,-22)
                }
            }
            
            Text("登录后即可使用APP")
                .padding(.top,4)
                .font(.system(size: 16))
                .foregroundStyle(Color(hex: "#FFFFFF"))
            
            Text(PhoneNumberMask.mask(phoneNumber))
                .padding(.top,24)
                .font(.system(size: 30))
                .foregroundStyle(Color(hex: "#FFFFFF"))
            Text("认证服务由\(compont)提供")
                .frame(height: 16)
                .padding(.top,24)
                .font(.system(size: 16))
                .foregroundStyle(Color(hex: "#FFFFFF"))
            
            HStack {
                Spacer()
                Button(action: {
                    if !isAgreed {
                        viewModel.error = "请同意用户服务协议"
                        return
                    }
                    viewModel.error = nil
//                    viewModel.loginByCode(mobile: viewModel.username, code: viewModel.code)
                }, label: {
                    HStack(alignment: .center, spacing: 0) {
                        Text("手机号登录/注册")
                            .font(.system(size: 16))
                            .foregroundColor(Color(hex: "#242424"))
                    }
                    .frame(maxWidth: .infinity, alignment: .center)
                })
                .padding()
                .alert(isPresented: $viewModel.isLoginSuccess, content: {
                    Alert(title: Text("提示"), message: Text("登录成功!"), dismissButton: .default(Text("确认"), action: {
                        viewModel.isLoginSuccess = false
                        viewModel.error = nil
                        appState.navigateTo(.tabbar)
                    })
                    )
                })
                Spacer()
            }
            .gradientBackground(colors: [Color(hex: "#00F3AF"),Color(hex: "#00E9DA")])
            .cornerRadius(8.0)
            .padding([.trailing,.leading],24)
            .padding(.top, 16)
            
            Button("其他登录") {
                appState.navigateTo(.passwordlogin)
            }.font(.system(size: 14))
                .foregroundColor(Color(hex: "#D5D5D5"))
                .padding(.top,16)
            
            Spacer()
            VStack {
                if WXApi.isWXAppInstalled() {
                    HStack {
                        VStack {
                            Divider()
                                .background(Color(hex: "#EEEEEE"))
                                .padding(.leading,43)
                        }
                        Text("第三方登录")
                            .font(.system(size: 14))
                            .foregroundColor(Color(hex: "#999999"))
                        VStack {
                            Divider()
                                .background(Color(hex: "#EEEEEE"))
                                .padding(.trailing,43)
                        }
                    }
                    HStack {
                        Spacer()
                        Image("wx")
                            .resizable()
                            .aspectRatio(contentMode: .fill)
                            .frame(width: 44,height: 44)
                            .onTapGesture {
                                if !isAgreed {
                                    viewModel.error = "请同意用户服务协议"
                                    return
                                }
                                weChatManager.requestLogin()
                            }
                        Spacer()
                   /*     Button(action: {
                            //苹果登录
                        }) {
                        }
                        .background(
                            Image("apple")  // 使用Assets中的图片
                                .resizable()
                                .aspectRatio(contentMode: .fill)
                                .frame(width: 44,height: 44)
                        )
                        Spacer()   */
                    }
                    .frame(height: 44)
                    .padding(.top,16)
                }
                HStack {
                    Toggle("", isOn: $isAgreed)
                        .toggleStyle(CustomToggleStyle())
                    Text("已阅读并同意")
                        .font(.system(size: 14))
                        .foregroundColor(Color(hex: "#888888"))
                    Spacer().frame(width: 0)
                    Button(action: {
                        //《用户服务协议》
                    }) {
                         Text("《用户服务协议》").foregroundColor(Color(hex: "#00E9DA"))
                            .font(.system(size: 14))
                            .foregroundColor(Color(hex: "#00E9DA"))
                            .padding(.vertical, 10)
                    }
                }
                .frame(height: 14)
                .padding(.top,30)
            }
            .padding(.bottom,47)
        }
        .navigationBarHidden(true)
        .background(Color(hex: "#0A0A0A"))
        .edgesIgnoringSafeArea(.all)
        .toast(isPresented: $showToast) {
            ShowMessageView(message: viewModel.error)
        }
        .onChange(of: viewModel.error) {
            newValue in
            if let errorMessage = newValue, !errorMessage.isEmpty {
                showToast = true
                DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
                    showToast = false
                    viewModel.error = nil
                }
            }
        }
        
        
        
    }
}

#Preview {
    NoCheckLonginView()
}
